#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <shadow.h>
#include <errno.h>
#include <iostream>

using std::cout;
using std::endl;


void get_setting(char * salt, char* passwd){
    int i,j;
    for(i=0, j=0; passwd[i] && j!= 4; ++i){
        if(passwd[i] == '$'){
            ++j;
        }
    }
    strncpy(salt,passwd,i);
}


int main(int argc, char *argv[])
{
    if(argc !=2){
        printf("第一个参数为用户名\n");
        exit(-1);
    }
    char setting[512] = {0};
    char * passwd = getpass("请输入密码：");
    printf("passwd: %s\n",passwd);

    //获取系统中的用户信息
    struct spwd * sp;
    if((sp = getspnam(argv[1]))==NULL){
        perror("getspnam");
        exit(-1);
    }
    get_setting(setting,sp->sp_pwdp);
    setting[strlen(setting)-1] = '\0';
    printf("setting : %s \n",setting);
    printf("sp_pwdp : %s\n",sp->sp_pwdp);
    
    char * encoded = crypt(passwd,setting);
    printf("endcoded : %s \n", encoded);
    printf("len: %ld\n",strlen(encoded));



    return 0;
}

